// pages/family/family.js
const app=getApp();
Page({
  onShareAppMessage() {
    return new Promise((resolve, reject) => {
      console.log("分享中");
      wx.login({
        success: (res) => {
          if (res.code) {
            const usercoden = res.code;
            resolve({
              title: '欢迎使用Ai小齿助手',
              path: '/pages/meinu/meinu?userid=' + usercoden+"&type=12",
            });
          } else {
            reject('登录失败，未获取到用户code');
          }
        },
        fail: (err) => {
          reject('wx.login 调用失败');
        }
      });
    });
  },
  data: {
    userinfo:{
      userurlid:0,
      userurl:'cloud://cloud1-9g2a0ajx3bbad7ff.636c-cloud1-9g2a0ajx3bbad7ff-1325147117/img/father.png',
      usergender:'性别',
      userrelation:'输入关系',
      userage:'年龄',
    },
    typeList:[],
    hidden:false,
    hiddenmodalput: true,
    nowP:0,
    //头像的index
    
  },
  changeHead1:function(e){
    const imageList =app.globalData.headUrlStander
    var p=e.currentTarget.dataset.id
    var thisp=this.data.typeList[p].userurlid
    //获取实体
    var mid=this.data.typeList[p]
    var total=this.data.typeList
    thisp+=1;
    if(thisp>=4)
    {
      thisp=0
    }
    var url=imageList[thisp]
    total[p].userurlid=thisp
    total[p].userurl=url
    this.setData({
      typeList: total
    });
    app.globalData.userList=total
  },
  onShow:function(){
    var mid=app.globalData.userList
    this.setData({
      typeList:mid
    })
  },
  //删除函数
  delet:function(e){
    var that=this
    wx.showModal({
      title: '再次确认是否删除记录',
      content: '',
      complete: (res) => {
        if (res.cancel) {
          
        }
    
        if (res.confirm) {
          var current = e.currentTarget.dataset.id;
          var list=that.data.typeList;
          list[current]=this.data.userinfo
          //删除清空记录
          list[current].userdignos=[]
          that.setData({
              typeList: list
        })
        app.globalData.userList=this.data.typeList
        const {avatarUrl,nickName} =wx.getStorageSync('userInfo')
        const openid=wx.getStorageSync('openid')
        db.collection('answer').where({
      _openid:openid
    }).get({
      success:(res)=>{
        const{data}=res
        if(data.length>0)
        {
          db.collection('answer').doc(data[0]._id).update
          ({
            data:{
              list:app.globalData.userList
            }
          })
        }
        else{
          db.collection('answer').add({
            data:{
              avatarUrl,
              nickName,
              list,
            }
          })
        }
      }
    })
        }
      }
    })
  },
  back:function(){
    wx.switchTab({
      url: "/pages/login/login",
    })
  },
  addType(){
    let list = this.data.typeList
    list.push(this.data.userinfo)
    this.setData({
      typeList:list
    })
  },
  fix:function(e){
    this.modalinput();
    var current = e.currentTarget.dataset.id;
    this.setData({
      nowP:current
    })
  },
  //修改
  modalinput: function () {
 
    this.setData({
 
      hiddenmodalput: !this.data.hiddenmodalput
 
    })
 
  },
 
  //取消按钮
 
  cancel: function () {
  
    this.setData({
      hiddenmodalput: true
    });
 
  },
 
  //确认
 
  confirm: function (e) {
    app.globalData.userList=this.data.typeList
    const {avatarUrl,nickName} =wx.getStorageSync('userInfo')
    const openid=wx.getStorageSync('openid')
    db.collection('answer').where({
      _openid:openid
    }).get({
      success:(res)=>{
        const{data}=res
        if(data.length>0)
        {
          db.collection('answer').doc(data[0]._id).update
          ({
            data:{
              list:app.globalData.userList
            }
          })
        }
        else{
          console.log("添加")
          var list=app.globalData.userList
          db.collection('answer').add({
            data:{
              avatarUrl,
              nickName,
              list,
            }
          })
        }
      }
    })
    this.setData({
      hiddenmodalput: true
    })
  },
  id:function(e){
    //var current = e.currentTarget.dataset.index;
    var list=this.data.typeList;
    list[this.data.nowP].userrelation=e.detail.value;
    
    this.setData({
      typeList:list
    })
  },
  age:function(e){
    var list=this.data.typeList;
    list[this.data.nowP].userage=e.detail.value;
    
    this.setData({
      typeList:list
    })
  },
  sex:function(e){
    var list=this.data.typeList;
    list[this.data.nowP].usergender=e.detail.value;
    
    this.setData({
      typeList:list
    })
  },
  onChangeShowState: function () {
 
    var that = this;
 
    that.setData({
 
      showView: (!that.data.showView)
 
    })
 
  },
  // 弹框
  powerDrawer: function (e) {
 
    var currentStatu = e.currentTarget.dataset.statu;
    console.log(currentStatu);
    this.util(currentStatu)
  },
  util: function (currentStatu) {
    /* 动画部分 */
    // 第1步：创建动画实例   
    var animation = wx.createAnimation({
      duration: 200, //动画时长  
      timingFunction: "linear", //线性  
      delay: 0 //0则不延迟  
    })
    // 第2步：这个动画实例赋给当前的动画实例  
    this.animation = animation;
 
    // 第3步：执行第一组动画  
    animation.opacity(0).rotateX(-100).step();
 
    // 第4步：导出动画对象赋给数据对象储存  
    this.setData({
      animationData: animation.export()
    })
 
    // 第5步：设置定时器到指定时候后，执行第二组动画  
    setTimeout(function () {
      // 执行第二组动画  
      animation.opacity(1).rotateX(0).step();
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象  
      this.setData({
        animationData: animation
      })
      //关闭  
      if (currentStatu == "close") {
 
        this.setData({
          showModalStatus: false
        });
        wx.showToast({
          title: '添加成功',
          icon: 'succes',
          duration: 1000,
          mask: true
        })
      }
    }.bind(this), 200)
    // 显示  
    if (currentStatu == "open") {
      this.setData({
        showModalStatus: true
      });
    }
  },
  
})